package selcoursesys.dao.impl;

import db.MyHibernateSessionFactory;

import selcoursesys.dao.BaseHibernateDAO;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.io.Serializable;
import java.util.Objects;


/**
 * Created by Lpbdz on 2017/4/8.
 */
public class BaseHibernateDAOImpl implements BaseHibernateDAO{
    private Session session;
    public void add(Object object){
        Transaction tran=null;
        session= MyHibernateSessionFactory.getSession();
        try{
            tran=session.beginTransaction();
            session.save(object);
            tran.commit();
        }catch (Exception e){
            if(tran!=null){
                tran.rollback();
            }e.printStackTrace();
        }finally {
            MyHibernateSessionFactory.closeSession();
        }
    }
    public Object get(Class cla, Serializable id){
        Object object=null;
        session=MyHibernateSessionFactory.getSession();
        try{
            object=session.get(cla,id);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyHibernateSessionFactory.closeSession();
        }
        return object;
    }
    public void delete(Object object){
        Transaction tran=null;
        session=MyHibernateSessionFactory.getSession();
        try{
            tran=session.beginTransaction();
            session.delete(object);
            tran.commit();
        }catch (Exception e){
            if(tran!=null){
                tran.rollback();
            }
            e.printStackTrace();
        }finally {
            MyHibernateSessionFactory.closeSession();
        }
    }
    public void Update(Object object){
        Transaction tran=null;
        session=MyHibernateSessionFactory.getSession();
        try{
            tran=session.beginTransaction();
            session.update(object);
            tran.commit();
        }catch (Exception e){
            if(tran!=null){
                tran.rollback();
            }
            e.printStackTrace();
        }finally {
            MyHibernateSessionFactory.closeSession();
        }
    }
}
